package cn.lili.controller.user.supplier;

import cn.lili.common.aop.annotation.DemoSite;
import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.member.entity.dto.UserEditDTO;
import cn.lili.modules.member.entity.dto.UserInfoDTO;
import cn.lili.modules.member.entity.dto.UserSearchParams;
import cn.lili.modules.member.entity.dto.UserStatusChangeDTO;
import cn.lili.modules.member.service.ManagerUserService;
import cn.lili.modules.permission.entity.vo.AdminUserVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 平台管理员接口
 *
 * @author Bulbasaur
 * @since 2020-02-25 14:10:16
 */
@RestController
@Api(tags = "店员管理接口")
@RequestMapping("/user/supplier")
@RequiredArgsConstructor
@Validated
public class SupplierUserController {

    private final ManagerUserService managerUserService;

    @ApiOperation(value = "管理员分页列表")
    @GetMapping
    public ResultMessage<Page<AdminUserVO>> getByPage(UserSearchParams userSearchParams) {
        AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
        if (SceneEnums.SUPPLIER.equals(currentUser.getScene())) {
            userSearchParams.setExtendId(currentUser.getExtendId());
        }
        userSearchParams.setScene(SceneEnums.SUPPLIER);
        return ResultUtil.data(managerUserService.getUserPage(userSearchParams, userSearchParams));
    }


    @ApiOperation(value = "通过ID获取管理员信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "/{id}")
    public ResultMessage<AdminUserVO> get(@PathVariable String id) {
        User user = managerUserService.getById(id);
        if ((user == null) || !SceneEnums.SUPPLIER.name().equals(user.getScene())) {
            return ResultUtil.data(new AdminUserVO());
        }
        return ResultUtil.data(new AdminUserVO(user));
    }

    @ApiOperation(value = "添加管理员")
    @PostMapping
    public ResultMessage<User> save(@Valid @RequestBody UserInfoDTO userInfoDTO) {
        AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
        if (SceneEnums.SUPPLIER.equals(currentUser.getScene())) {
            userInfoDTO.setExtendId(currentUser.getExtendId());
        }
        userInfoDTO.setScene(SceneEnums.SUPPLIER);
        return ResultUtil.data(managerUserService.addUser(userInfoDTO));
    }

    @DemoSite
    @PreventDuplicateSubmissions
    @ApiOperation(value = "修改管理员基本信息")
    @PutMapping
    public ResultMessage<AdminUserVO> update(@Valid @RequestBody UserEditDTO userEditDTO) {
        AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
        if (SceneEnums.SUPPLIER.equals(currentUser.getScene())) {
            userEditDTO.setExtendId(currentUser.getExtendId());
        }
        userEditDTO.setScene(SceneEnums.SUPPLIER);
        return ResultUtil.data(managerUserService.updateUser(userEditDTO));
    }

    @DemoSite
    @PreventDuplicateSubmissions
    @ApiOperation(value = "管理员状态修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userIds", value = "用户ID", required = true, dataType = "String", allowMultiple = true, paramType = "query"),
            @ApiImplicitParam(name = "enable", required = true, dataType = "boolean", paramType = "query")
    })
    @PutMapping("/status")
    public ResultMessage<Object> updateMemberStatus(@Valid UserStatusChangeDTO userStatusChangeDTO) {
        AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
        if (SceneEnums.SUPPLIER.equals(currentUser.getScene())) {
            userStatusChangeDTO.setExtendId(currentUser.getExtendId());
        }
        if (userStatusChangeDTO.getUserIds().contains(currentUser.getId())) {
            throw new ServiceException("不能修改自己的状态");
        }
        userStatusChangeDTO.setScene(SceneEnums.SUPPLIER);
        managerUserService.updateUserStatus(userStatusChangeDTO);
        return ResultUtil.success();
    }


    @ApiOperation(value = "管理员删除")
    @DeleteMapping("/{ids}")
    public ResultMessage<Object> delete(@PathVariable List<String> ids) {
        AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
        UserStatusChangeDTO userStatusChangeDTO = new UserStatusChangeDTO();
        if (currentUser.getScene().equals(SceneEnums.SUPPLIER)) {
            userStatusChangeDTO.setExtendId(currentUser.getExtendId());
        }
        if (ids.contains(currentUser.getId())) {
            throw new ServiceException("不能删除自己");
        }
        userStatusChangeDTO.setScene(SceneEnums.SUPPLIER);
        userStatusChangeDTO.setUserIds(ids);
        return ResultUtil.data(managerUserService.removeUser(userStatusChangeDTO));
    }


    @ApiOperation(value = "管理员重置密码")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
    @PutMapping("/reset/{ids}")
    public ResultMessage<Object> resetPassword(@PathVariable List<String> ids) {
        managerUserService.resetPassword(ids);
        return ResultUtil.success();
    }

    @ApiOperation(value = "根据条件查询管理员总数")
    @GetMapping("/num")
    public ResultMessage<Long> getNum(UserSearchParams userSearchParams) {
        AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
        if (SceneEnums.SUPPLIER.equals(currentUser.getScene())) {
            userSearchParams.setExtendId(currentUser.getExtendId());
        }
        userSearchParams.setScene(SceneEnums.SUPPLIER);
        return ResultUtil.data(managerUserService.getMemberNum(userSearchParams));
    }


}
